package com.llkj.qingnianguoxue.adapter;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.bean.EveryDayBean;
import com.llkj.qingnianguoxue.util.ClickViewCallback;
import com.llkj.qingnianguoxue.util.DateUtils;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.StringUtils;

import java.util.List;

/**
 * 每日进一步
 */
public class EveryDayAdapter extends BaseQuickAdapter<EveryDayBean.DayBean, BaseViewHolder> {
    private ClickViewCallback listener;

    public EveryDayAdapter(int layoutResId, List<EveryDayBean.DayBean> data, ClickViewCallback listener) {
        super(layoutResId, data);
        this.listener = listener;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, EveryDayBean.DayBean item) {
        RelativeLayout rlView2 = baseViewHolder.getView(R.id.rl_item);
        setListener(baseViewHolder.getLayoutPosition(), rlView2);
        int type = item.getType();// 类型 1 视频2 图书 3 天

        ImageView ivPic = baseViewHolder.getView(R.id.pic);
        baseViewHolder.setText(R.id.tv_title, item.getTitle());//
        baseViewHolder.setText(R.id.tv_introduction, item.getShare());

        if(StringUtils.isEmpty(item.getPrice())){
            baseViewHolder.setText(R.id.tv_price, "");
        }else{
            baseViewHolder.setText(R.id.tv_price, "￥" + item.getPrice()+"银子");
        }

        if(!StringUtils.isEmpty(item.getPlays_percent()) && item.getPlays_percent().contains("%")){
            baseViewHolder.setText(R.id.tv_progress, "观看至" + item.getPlays_percent());
        }else if(StringUtils.isEmpty(item.getPlays_percent())){
            baseViewHolder.setText(R.id.tv_progress, "观看至" + "0%");
        }else{
            baseViewHolder.setText(R.id.tv_progress, "观看至" + item.getPlays_percent()+"%");
        }

        baseViewHolder.setText(R.id.tv_date, DateUtils.getLastScanTime(item.getAddtime()));//上次观看时间
        FrameLayout.LayoutParams picParam = (FrameLayout.LayoutParams) ivPic.getLayoutParams();
        ImageView ivPlay = baseViewHolder.getView(R.id.iv_video_play);

        ivPic.setScaleType(ImageView.ScaleType.FIT_XY);
        if (type == 1) {//视频
            ivPlay.setVisibility(View.VISIBLE);
            baseViewHolder.setText(R.id.tv_video_time, item.getDuration());//时间
            picParam.width = DisplayUtil.getDensityWdith(mContext) * 268 / 750;
            picParam.height = picParam.width * 9 / 16;

            if("1".equals(item.getIs_order()) || StringUtils.isEmpty(item.getIs_order())){
                baseViewHolder.setText(R.id.tv_price, "");
            }
        } else if (type == 2) {//图书
            ivPlay.setVisibility(View.INVISIBLE);
            picParam.width = DisplayUtil.getDensityWdith(mContext) * 65 / 320;
            picParam.height = picParam.width * 14/ 13;
        }
        ivPic.setLayoutParams(picParam);
        ImageLoadingUtil.loadImgGlideRoundCenterCrop(ivPic, item.getImg());

        TextView tvDelItem = baseViewHolder.getView(R.id.tvDelItem);
        tvDelItem.getLayoutParams().height = picParam.height+DisplayUtil.dip2px(mContext,20);
        baseViewHolder.addOnClickListener(R.id.tvDelItem);
        SwipeRevealLayout swipeDelGroup = baseViewHolder.getView(R.id.swipeDelGroup);
        swipeDelGroup.close(true);
        swipeDelGroup.setSwipeListener(new SwipeRevealLayout.SimpleSwipeListener(){
            @Override
            public void onClosed(SwipeRevealLayout view) {
                super.onClosed(view);
            }

            @Override
            public void onOpened(SwipeRevealLayout view) {
                super.onOpened(view);
            }
        });
    }

    public void setListener(final int position, final View v) {
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.clickViewCallback(position, 0, v);
                }
            }
        });
    }

}
